local jinzun = fk.CreateSkill {
  name = "rmt__jinzun",
  tags = { Skill.Compulsory },
}

Fk:loadTranslationTable{
  ["rmt__jinzun"] = "金樽",
  [":rmt__jinzun"] = "锁定技，你的体力值不提供手牌上限，改为提供额定摸牌数；你的武器技能失效，改为“本局游戏限X次，你可以视为使用一张【酒】（X为此牌攻击范围）”的效果（重新装备不刷新，同牌名共用次数）。",
}

jinzun:addEffect(fk.DrawNCards, {
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(jinzun.name)
  end,
  on_use = function(self, event, target, player, data)
    data.n = data.n + player.hp
  end,
})

jinzun:addEffect("maxcards", {
  correct_func = function(self, player)
    if player:hasSkill(jinzun.name) then
      return - player.hp
    end
  end,
})

jinzun:addEffect(fk.AfterCardsMove, {
  can_refresh = function(self, event, target, player, data)
    if not player:hasSkill(jinzun.name) then return false end
    for _, move in ipairs(data) do
      for _, info in ipairs(move.moveInfo) do
        if (move.to == player and move.toArea == Card.PlayerEquip)
        or (move.from == player and info.fromArea == Card.PlayerEquip) then
          return Fk:getCardById(info.cardId).sub_type == Card.SubtypeWeapon
        end
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    for _, move in ipairs(data) do
      for _, info in ipairs(move.moveInfo) do
        if move.to == player and move.toArea == Card.PlayerEquip and Fk:getCardById(info.cardId).sub_type == Card.SubtypeWeapon then
          for _, skill in ipairs(player.player_skills) do
            if skill:getSkeleton().attached_equip and Fk.all_card_types[skill:getSkeleton().attached_equip].sub_type == Card.SubtypeWeapon then
              if Fk.skills[skill] and player:hasSkill(skill, true) then
                player.room:handleAddLoseSkills(player, "-"..skill, jinzun.name, true, false)
              end
              if Fk.skills[skill.name] and player:hasSkill(skill.name, true) then
                player.room:handleAddLoseSkills(player, "-"..skill.name, jinzun.name, true, false)
              end
            end
          end
          local get = true
          for i = 0, 30, 1 do
            local name = i == 0 and "rmt__jinzunjiu" or "rmt"..i.."__jinzunjiu"
            if not get then break end
            if player:getMark(""..name.."record") == Fk:getCardById(info.cardId).name then
              get = false
              player.room:handleAddLoseSkills(player, name, nil, true, false)
              break
            end
          end
          for i = 0, 30, 1 do
            local name = i == 0 and "rmt__jinzunjiu" or "rmt"..i.."__jinzunjiu"
            if not get then break end
            if not player:hasSkill(name, true) and player:getMark(""..name.."record") == 0 then --防止顶掉本局已有的记录
              get = false
              --player:setSkillUseHistory(name, 0, Player.HistoryGame)
              player.room:setPlayerMark(player, ""..name, Fk:getCardById(info.cardId).attack_range ~= nil and Fk:getCardById(info.cardId).attack_range or 1)
              player.room:setPlayerMark(player, ""..name.."record", Fk:getCardById(info.cardId).name) --记录一下卡牌名字，用来定位删除的技能
              player.room:handleAddLoseSkills(player, name, nil, true, false)
              break
            end
          end
        elseif move.from == player and info.fromArea == Card.PlayerEquip and Fk:getCardById(info.cardId).sub_type == Card.SubtypeWeapon then
          for i = 0, 30, 1 do
            local name = i == 0 and "rmt__jinzunjiu" or "rmt"..i.."__jinzunjiu"
            if player:hasSkill(name, true) and player:getMark(""..name.."record") == Fk:getCardById(info.cardId).name then
              --player:setSkillUseHistory(name, 0, Player.HistoryGame)
              --player.room:setPlayerMark(player, ""..name, 0)
              --player.room:setPlayerMark(player, ""..name.."record", 0)
              player.room:handleAddLoseSkills(player, "-"..name, nil, true, false)
              break
            end
          end
        end
      end
    end
  end,
})

jinzun:addAcquireEffect(function (self, player, is_start)
  for _, skill in ipairs(player.player_skills) do
    if skill:getSkeleton().attached_equip and Fk.all_card_types[skill:getSkeleton().attached_equip].sub_type == Card.SubtypeWeapon then
      if Fk.skills[skill] and player:hasSkill(skill, true) then
        player.room:handleAddLoseSkills(player, "-"..skill, jinzun.name, true, false)
      end
      if Fk.skills[skill.name] and player:hasSkill(skill.name, true) then
        player.room:handleAddLoseSkills(player, "-"..skill.name, jinzun.name, true, false)
      end
    end
  end
  for _, e in ipairs(player.player_cards[Player.Equip]) do
    if Fk:getCardById(e).sub_type == Card.SubtypeWeapon then
      local get = true
      for i = 0, 30, 1 do
        local name = i == 0 and "rmt__jinzunjiu" or "rmt"..i.."__jinzunjiu"
        if not get then break end
        if player:getMark(""..name.."record") == Fk:getCardById(e).name then
          get = false
          player.room:handleAddLoseSkills(player, name, nil, true, false)
          break
        end
      end
      for i = 0, 30, 1 do
        local name = i == 0 and "rmt__jinzunjiu" or "rmt"..i.."__jinzunjiu"
        if not player:hasSkill(name, true) and player:getMark(""..name.."record") == 0 then --防止顶掉本局已有的记录
          get = false
          --player:setSkillUseHistory(name, 0, Player.HistoryGame)
          player.room:setPlayerMark(player, ""..name, Fk:getCardById(e).attack_range ~= nil and Fk:getCardById(e).attack_range or 1)
          player.room:setPlayerMark(player, ""..name.."record", Fk:getCardById(e).name) --记录一下卡牌名字，用来定位删除的技能
          player.room:handleAddLoseSkills(player, name, nil, true, false)
          break
        end
      end 
    end
  end
end)

jinzun:addLoseEffect(function (self, player, is_death)
  for i = 0, 30, 1 do
    local name = i == 0 and "rmt__jinzunjiu" or "rmt"..i.."__jinzunjiu"
    if player:hasSkill(name, true) then
      --player:setSkillUseHistory(name, 0, Player.HistoryGame)
      --player.room:setPlayerMark(player, ""..name, 0)
      --player.room:setPlayerMark(player, ""..name.."record", 0)
      player.room:handleAddLoseSkills(player, "-"..name, nil, true, false)
    end
  end
end)

jinzun:addEffect('invalidity', {
  invalidity_func = function(self, player, skill)
    if skill:getSkeleton() and skill:getSkeleton().attached_equip and
      Fk:cloneCard(skill:getSkeleton().attached_equip).sub_type == Card.SubtypeWeapon then
      if player:hasSkill(jinzun.name) then return true end
    end
  end
})

return jinzun
